const fs = require('fs')
const path = require('path')

function readFile(name) {
  return new Promise((resolve, reject) => {
    if(typeof name === 'string') {
      readFileSingle(name).then(data => {
        resolve(data)
      })
    }else if(typeof name === 'object') {
      Promise.all(name.map(item => readFileSingle(item))).then(data => {
        resolve(data)
      })
    }
  })
}

function readFileSingle(name) {
  return new Promise((resolve, reject) => {
    fs.readFile(path.resolve(__dirname,`../../public/data/${name}.json`), 'utf-8',(err, data) => {
      if(err) throw err
      resolve(data)
    })
  })
}

module.exports = readFile